﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proyecta.Data.Helpers
{
    public class QueryCondition : IGenerableQuery
    {
        public LogicOperator Operador { get; set; }
        public bool Negar { get; set; }
        public string Expresion1 { get; set; }
        public CompareOperator Comparacion { get; set; }
        public string Expresion2 { get; set; }

        private static string[] operadores = { "=", "<>", "<", "<=", ">", ">=", "IS", "LIKE" };

        internal QueryCondition() { }

        internal QueryCondition(string exp1, CompareOperator comparacion, string exp2)
        {
            this.Expresion1 = exp1;
            this.Comparacion = comparacion;
            this.Expresion2 = exp2;
        }

        internal QueryCondition(bool negar, string exp1, CompareOperator comparacion, string exp2)
        {
            this.Negar = negar;
            this.Expresion1 = exp1;
            this.Comparacion = comparacion;
            this.Expresion2 = exp2;
        }

        internal QueryCondition(LogicOperator operador, string exp1, CompareOperator comparacion, string exp2)
        {
            this.Operador = operador;
            this.Expresion1 = exp1;
            this.Comparacion = comparacion;
            this.Expresion2 = exp2;
        }

        internal QueryCondition(LogicOperator operador, bool negar, string exp1, CompareOperator comparacion, string exp2)
        {
            this.Operador = operador;
            this.Negar = negar;
            this.Expresion1 = exp1;
            this.Comparacion = comparacion;
            this.Expresion2 = exp2;
        }

        public void Generate(StringBuilder sql)
        {
            if (this.Negar) sql.Append("NOT ");
            sql.AppendFormat("{0} {1} {2}", this.Expresion1, operadores[(int)this.Comparacion], this.Expresion2);
        }
    }
}